package net.searchdata.pre;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.junit.Test;

public class Entry28Two {
	public static <E> Set<E> union(Set<E> set1, Set<E> set2) {
		Set<E> set = new HashSet<E>(set1);
		set.addAll(set2);

		return set;
	}

	public static <T extends Comparable<T>> T max(List<T> list) {
		Iterator<? extends T> it = list.iterator();
		T result = it.next();

		while (it.hasNext()) {
			T t = it.next();
			result = (result.compareTo(t) > 0) ? result : t;
		}

		return result;
	}

	public static <E> void swap(List<E> list, int i, int j) {
		list.set(i, list.set(j, list.get(i)));
	}

	@Test
	public void testUnion() {
		Set<Integer> intSet1 = new HashSet<Integer>();
		Set<Integer> intSet2 = new HashSet<Integer>();

		intSet1.addAll(Arrays.asList(1, 2, 3));
		intSet2.addAll(Arrays.asList(3, 4, 5));

		Set<Integer> set = union(intSet1, intSet2);
		assertThat(set.size(), is(5));
	}
}
